<?php

namespace App\Service;

use App\Models\EwSms;

class SmsService extends BaseService
{
	
	public static function getList($params, $count = FALSE)
	{
		$query = EwSms::query();
		$query->columns(['DISTINCT(id) AS id,mobile,content,create_time,error']);

		//mobile
		if (isset($params->mobile) && $params->mobile ){
			$query->andwhere("mobile LIKE '%" . $params->mobile . "%'");
		}

		//content
		if (isset($params->content) && $params->content ){
			$query->andwhere("content LIKE '%" . $params->content . "%'");
		}

		//error
		if (isset($params->error) && $params->error ){
			$query->andwhere("error LIKE '%" . $params->error . "%'");
		}

		//总条数
		if ($count)
			return $query->execute()->count();

		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by);
			$query->orderBy($sql);
		}

		//分页
		$query->limit($params->page_num,($params->p-1)*$params->page_num);

		return $query->execute();
	}

	public static function getInfo($id = 0)
	{
		return EwSms::findFirst($id);
	}

	public static function count()
	{
		return EwSms::count();
	}

	/**
	 * [delete 彻底删除]
	 * @param  [type] $id [description]
	 * @return [type]     [description]
	 */
	public static function forever($id)
	{
		if (isset($id) && $id)
			$row = EwSms::findFirst($id);

		if ($row)
			return $row->delete();

		return NULL;
	}

	/**
	 * [save 保存]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$row = new EwSms;
		if (isset($data->id) && $data->id)
			$row = EwSms::findFirst($data->id);

		return $row->save((array)$data);
	}
}